package com.xiaolin.utils;

import com.xiaolin.bean.User;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.Properties;

public class test {
    public static void main(String[] args) throws Exception {
        InputStream is = test.class.getClassLoader().getResourceAsStream("dbutils.properties");
        Properties pro = new Properties();
        pro.load(is);
        //赋值
        String driver = pro.getProperty("driverClass");
        String url = pro.getProperty("url");
        String username = pro.getProperty("user");
        String password = pro.getProperty("password");
        Class.forName(driver);        //反射的方式注册驱动
        Connection conn = DriverManager.getConnection(url, username, password);
        String sql = "SELECT acNum,password,name,age,email,phone FROM user WHERE acNum=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1, "1");
        ResultSet rs = ps.executeQuery();
        // 获取结果集的源数据
        ResultSetMetaData rsmd = rs.getMetaData();
        // 通过ResultSetMetadata获取结果集中的列数
        int columnCount = rsmd.getColumnCount();
        User us = new User();
        while (rs.next()) {
            for (int i = 0; i < columnCount; i++) {
                // 获取列值
                Object columValue = rs.getObject(i + 1);
                //获取每个列的列名
                String columnLabel = rsmd.getColumnLabel(i + 1);
                System.out.println(columnLabel);
                System.out.println(columnLabel + "   " + columValue);
            }
        }
    }
}
